import Cookies from "js-cookie";

const state = {
  /* 设备信息 */
  device: "desktop",
  /* admin页面的sidebar控制信息 */
  sidebar: {
    opened: true,
    isShowLogo: true,
    withoutAnimation: false,
  },
  /* admin页面是否固定顶部 */
  isFixedHeader: true,
};

const mutations = {
  /* 设备信息 */
  TOGGLE_DEVICE: (state, device) => {
    state.device = device;
  },
  /* sidebar控制 */
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    // 设置cookie信息
    if (state.sidebar.opened) {
      Cookies.set("sidebar-status", 1);
    } else {
      Cookies.set("sidebar-status", 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set("sidebar-status", 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
};

const actions = {
  /* 设备信息 */
  toggleDevice({ commit }, device) {
    commit("TOGGLE_DEVICE", device);
  },
  /* sidebar控制 */
  toggleSidebar({ commit }) {
    commit("TOGGLE_SIDEBAR");
  },
  closeSidebar({ commit }, { withoutAnimation }) {
    commit("CLOSE_SIDEBAR", withoutAnimation);
  },
  /* header控制 */
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
